<?php
require_once("./define/define_table.php");
define("ADD_ROLE_FORM", "Formulaire d'ajout d'un role");
define("MODIFY_ROLE_FORM", "Formulaire de modification d'un role");
class Role{

    private $id_role;
    private $nom;

    public function Role(){

    }

    public function getId(){
        return $this->id_role;
    }

    public function getNom(){
        return $this->nom;
    }
    
    public function setId($id){
        $this->id_role = $id;
    }
    
    public function setNom($nom){
        $this->nom = $nom;
    }

    public function getAllRole(){
        global $db;
        try{
            $sql = "SELECT id_role, nom FROM ".ROLE_TABLE;
            $sth = $db->prepare($sql);
            $sth->execute();
            PrintQuery::queryPrint($sth);
            return $sth->fetchAll(PDO::FETCH_CLASS, "Role");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }
    
    public function getRole($id){
        global $db;
        try{
            $sql = "SELECT id_role, nom FROM ".ROLE_TABLE."
            		WHERE id_role = :id_role";
            $sth = $db->prepare($sql);
            $sth->bindValue(":id_role", $id, PDO::PARAM_INT);
            $sth->execute();
            return $sth->fetchObject("Role");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }
    
    public function addOrModify(){
        global $db;
        try{
            if($this->id_role == null){
                $sql = "INSERT INTO ".ROLE_TABLE." VALUES(null, :nom)";
                $sth = $db->prepare($sql);
                $sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
            }
            else{
                $sql = "UPDATE ".ROLE_TABLE." SET nom = :nom WHERE id_role = :id_role";
                $sth = $db->prepare($sql);
                $sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
                $sth->bindValue(":id_role", $this->id_role, PDO::PARAM_INT);
            }
            PrintQuery::queryPrint($sth);
            return $sth->execute();
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }
    
    public static function getAddForm(){
        $html = ADD_ROLE_FORM .
    				'<form action="?module=role&action=doAdd" method="post">' .
    				'	<table>' .
    				'		<tr>' .
    				'			<td>'._NOM_ROLE_.'</td>' .
    				'			<td><input type="text" name="nom" /></td>' .
    				'		</tr>' .
    				'		<tr>' .
    				'			<td colspan="2"><input type="submit" />' .
    				'		</tr>' .
    				'	</table>' .
    				'</form>';
        return $html;
    }
    
    public function getModifyForm(){
        $html = MODIFY_ROLE_FORM .
    				'<form action="?module=role&action=doModify" method="post">' .
    				'	<table>' .
    				'		<tr>' .
    				'			<td>'._ID_ROLE_.'</td>' .
    				'			<td><input readonly type="text" name="id_role" value="'.$this->id_role.'"/></td>' .
    				'		</tr>' .
    				'		<tr>' .
    				'			<td>'._NOM_ROLE_.'</td>' .
    				'			<td><input type="text" name="nom" value="'.$this->nom.'" /></td>' .
    				'		</tr>' .
    				'		<tr>' .
    				'			<td colspan="2"><input type="submit" />' .
    				'		</tr>' .
    				'	</table>' .
    				'</form>';
        return $html;
    }
    
    public function delete(){
        global $db;
        $sql = "DELETE FROM ".ROLE_TABLE." WHERE id_role = :id_role";
        $sth = $db->prepare($sql);
        $sth->bindValue(":id_role", $this->id_role, PDO::PARAM_INT);
        $sth->execute();
        PrintQuery::queryPrint($sth);
    }
    
    public function generateSelect($id = null){
            $array = self::getAllRole();
            $html = '<select name="id_role">';
            foreach($array as $i){
                $selected = ($i->getId() == $id) ?  'selected="selected"' : '';
                $html .= '<option value="'.$i->getId().'" '.$selected.'>'.$i->getNom().'</option>';
            }
            $html .= '</select>';
            return $html;
    }
}
?>
